<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Car extends Model
{
    use SoftDeletes;
    protected $table = 'car';
    protected $casts = [
        'license_driving' => 'array',
    ];
    /**
     * 所属公司
     */
    public function company()
    {
      $this->hasOne(Company::class,'id','company_id');
    }

//    /**
//     * 司机
//     */
//    public function driver()
//    {
//      $this->hasOne(Drivers::class,'id','driver_id');
//    }

    /**
     * 司机车辆关联表
     */
    public function driver_car()
    {
        $this->hasOne(DriverCar::class,'id','driver_id');
    }

    /**
     * 车辆品牌
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @Author 姿势就是力量
     */
    public function brand()
    {
        return $this->belongsTo(CarBrand::class, 'brand_id', 'id');
    }



    /**
     * 车辆所属司机
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
     * @Author 姿势就是力量
     */
    public function driver()
    {
        return $this->hasManyThrough(
            Drivers::class,
            DriverCar::class,
            'car_id',
            'id',
            'id',
            'driver_id'
        );
    }


    /**
     * 车辆正面
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function front()
    {
        return $this->hasOne(UploadFile::class, 'id', 'front')->select(['id', 'file_url', 'file_name']);
    }


    /**
     * 车辆侧面
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function side()
    {
        return $this->hasOne(UploadFile::class, 'id', 'side')->select(['id', 'file_url', 'file_name']);
    }


    /**
     * 车辆背面
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * @Author 姿势就是力量
     */
    public function back()
    {
        return $this->hasOne(UploadFile::class, 'id', 'back')->select(['id', 'file_url', 'file_name']);
    }


    /**
     * 业务类型
     */
    public function service()
    {
        return $this->belongsTo(ServiceType::class, 'type', 'id');
    }

    public function upload_file()
    {
        return $this->belongsTo(UploadFile::class, 'id', 'license_driving');
    }

    public function service_type()
    {
        return $this->hasOne(ServiceType::class,'id','type');
    }
}
